package com.pactera.madp.cp.api.util;


import java.time.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Day {

    private Calendar instance;

    private LocalDate localDate;

    Calendar getInstance() {
        instance.set(Calendar.HOUR_OF_DAY, 0);
        instance.set(Calendar.MINUTE, 0);
        instance.set(Calendar.SECOND, 0);
        instance.set(Calendar.MILLISECOND, 0);
        return (Calendar) instance.clone();
    }

    public LocalDate getFirstDayOfWeek() {
        Calendar instance = getInstance();
        instance.set(Calendar.DAY_OF_WEEK, 2);
        return instance.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public LocalDate getFirstDayOfMonth() {
        Calendar instance = getInstance();
        instance.set(Calendar.DAY_OF_MONTH, 1);
        return instance.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }
    public Boolean isWeekEnd() {
        // 周天对应着每周第一天
        return this.localDate.getDayOfWeek().getValue() == 1;
    }
    public Boolean isMonthEnd() {
        // 周天对应着每周第一天
        return this.localDate.getDayOfWeek().getValue() == 1;
    }
    public LocalDate getFirstWeekDay() {
        LocalDate week;
        int value = this.localDate.getDayOfWeek().getValue();
        // 周天减6 周一（2）到周六（7）少减个2
        if (value == 1)
            week = this.localDate.minusDays(value - 6); // 如果是周天则定位到本周第一天
        else
            week = this.localDate.minusDays(value - 2);
        return week;
    }

    public LocalDate getFirstMonthDay() {
        return this.localDate.minusDays(localDate.getDayOfMonth() - 1);
    }

    public Day(Calendar instance) {
        this.instance = instance;
        this.localDate = instance.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }
}
